﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using NetSurveillance.WebApp.Binders;
using NetSurveillance.WebApp.Filters;
using NetSurveillance.WebApp.Foundations;
using NetSurveillance.WebApp.Utilities;

namespace NetSurveillance.WebApp
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
            filters.Add(new CultureAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Localization",
                "{lang}/{controller}/{action}/{id}",
                new { controller = "Home", action = "Index", id = UrlParameter.Optional },
                new { lang = @"\w{2}(?:-\w{2})?" }
                );

            routes.MapRoute(
               "Default", // Route name
               "{controller}/{action}/{id}", // URL with parameters
               new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
           );
        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);

            PersistentDataContainer.LoadCurrent = (() => new SessionContainer());
            PersistentDataContainer.LoadCurrentCache = (() => new CacheContainer());
            PersistentDataContainer.LoadCurrentApplication = (() => new ApplicationContainer());

            //加载Xml资源文件，用于ActionCode替换
            XmlResource.LoadXmlResourceData(Server.MapPath("~/XmlResource"), "Resource");

            //加载支持的协议和字段
            SupportUtility.LoadSupportData(Server.MapPath("~/XmlResource"), "Supports");

            //加载Interactive的错误资源
            XmlResource.LoadItactCodeEles(Server.MapPath("~/XmlResource/InteractiveCode.xml"));
        }
    }
}